package qeug.shsh.fi.fromPlugin;

import kd.bos.context.RequestContext;
import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.entity.datamodel.events.ChangeData;
import kd.bos.entity.datamodel.events.PropertyChangedArgs;
import kd.bos.form.plugin.AbstractFormPlugin;
import kd.bos.servicehelper.BusinessDataServiceHelper;
import kd.bos.servicehelper.user.UserService;
import kd.bos.servicehelper.user.UserServiceHelper;
import kd.occ.ocdbd.mservice.CUserUpgradeServiceImpl;
import kd.bos.dataentity.utils.StringUtils;

import java.time.LocalDate;
import java.time.ZoneId;
import java.util.Calendar;
import java.util.Date;
import java.util.EventObject;

public class FundPlanFromPlugin extends AbstractFormPlugin {

    @Override
    public void afterCreateNewData(EventObject e) {
        super.afterCreateNewData(e);
        String userid=RequestContext.get().getUserId();
        long  useridint=Long.valueOf(userid);
        long  mainOrgID=UserServiceHelper.getUserMainOrgId(Long.valueOf(userid));
        DynamicObject userInfo = BusinessDataServiceHelper.loadSingle(useridint,"bos_user", "id,number,name");
        DynamicObject adminInfo = BusinessDataServiceHelper.loadSingle(mainOrgID, "bos_adminorg", "id,number,name");
        this.getModel().setValue("qeug_sqperson", userInfo);//申请人
        this.getModel().setValue("qeug_deparment", adminInfo);//部门
//        this.getModel().setValue("org", null);//公司


    }

    private final static String qeug_sqperson = "qeug_sqperson";//申请人
    @Override
    public void propertyChanged(PropertyChangedArgs e) {
        String fieldKey = e.getProperty().getName();
        if (StringUtils.equals(qeug_sqperson, fieldKey)) {
            DynamicObject sqpersonInfo = (DynamicObject) this.getModel().getValue("qeug_sqperson");
            if(sqpersonInfo!=null){
                long  useridint=Long.valueOf(sqpersonInfo.get("id").toString());
                long  mainOrgID=UserServiceHelper.getUserMainOrgId(Long.valueOf(useridint));
                DynamicObject adminInfo = BusinessDataServiceHelper.loadSingle(mainOrgID, "bos_adminorg", "id,number,name");
                this.getModel().setValue("qeug_deparment", adminInfo);//部门
            }
        }else if(StringUtils.equals("qeug_ysdate",fieldKey)){
            //预算日期
            //修改预算日期赋值预算周
            ChangeData[] changeSet = e.getChangeSet();
            ChangeData changeData = changeSet[0];
            Date ysDate = (Date) changeData.getNewValue();//预算日期

            // 将预算日期转换为周计划格式
            String weekPlan = getWeekPlanFromDate(ysDate);

            // 将周计划赋值给目标字段
            this.getModel().setValue("qeug_week1", weekPlan);//周计划
        }
    }
    private String getWeekPlanFromDate(Date date) {
        // 将Date转换为LocalDate
        LocalDate localDate = date.toInstant().atZone(ZoneId.systemDefault()).toLocalDate();
        int year = localDate.getYear();

        // 获取该年1月1日的星期几（周一=1，周日=7）
        LocalDate firstDayOfYear = LocalDate.of(year, 1, 1);
        int weekday = firstDayOfYear.getDayOfWeek().getValue();

        // 计算第一周的天数
        int firstWeekDays = 8 - weekday;
        if (firstWeekDays > 7) { // 处理周日的情况，当weekday为7时，firstWeekDays=1
            firstWeekDays = 1;
        }

        int dayOfYear = localDate.getDayOfYear();
        int weekNumber;

        if (dayOfYear <= firstWeekDays) {
            // 如果在第一周的天数范围内
            weekNumber = 1;
        } else {
            // 计算剩余天数并确定周数
            int remainingDays = dayOfYear - firstWeekDays;
            weekNumber = 1 + (remainingDays + 6) / 7;
        }

        // 格式化为两位数的周数
        String formattedWeek = String.format("%02d", weekNumber);
        return String.format("%d-%s", year, formattedWeek);
    }
}
